#include "stdafx.h"
#include "IAdd.h"
//OpenCV libs
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv/cv.h"

const char * const IAdd::FunctionNameIAdd = "Add";

IAdd::IAdd(void) 
{
	_fnApi.SetFunctionName(IAdd::FunctionNameIAdd);
	_fnApi.SetFunctionDescription("This function adds a image on top of the solution.");
									//inputs
	_fnApi.AddParamDef(new ProcParamDefString(0, "Image", "Sets the Image to be added. Make sure the dimensions are the same as that of the image you're adding to. Enter the path to the image", ProcParamDef::input, 0, 500 ));
									//outputs
	_fnApi.AddParamDef(new ProcParamDefInt(0, ProcParam::attribOutputParamNameResult, "0 is success", ProcParamDef::output, 0, 1));

	_fnApi.Validate();
}


IAdd::~IAdd(void)
{

}


bool IAdd::Process(ImageProcStep *step, bool DebugMode)
{	
	bool rc = false;
	if ((step != '\0') && (step->IsValid()))
	{
		
		ProcParamString *source = dynamic_cast<ProcParamString *>(step->GetInputs()->GetParam(0));	//Check the Param number

		if  (source != '\0')
		{
			cv::Mat output;
			cv::Mat input = cv::imread(step->GetIntermediateImagePathFilename());	
			cv::Mat input2 = cv::imread((std::string)source->GetValueStr());

			if (input.cols == input2.cols && input.rows == input2. rows)
			{
				cv::add(input, input2, output);

				cv::imwrite(step->GetIntermediateImagePathFilename(), output);
				
				input.release();
				input.release();
				output.release();
				rc = true;
			}
		}
	}
	return rc;
}

